package cn.org.donald.controller.game;


import cn.org.donald.framework.constant.InfoConstant;
import cn.org.donald.framework.handler.ServiceException;
import cn.org.donald.framework.pojo.vo.HttpResult;
import cn.org.donald.framework.pojo.vo.TableResult;
import cn.org.donald.pojo.dto.game.GameFeatureDto;
import cn.org.donald.pojo.entity.game.Feature;
import cn.org.donald.pojo.entity.game.Game;
import cn.org.donald.pojo.vo.GameVo;
import cn.org.donald.service.game.GameFeatureService;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import cn.org.donald.framework.base.BaseController;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Donald
 * @since 2020-12-12
 */
@RestController
@RequestMapping("/game/game-feature")
public class GameFeatureController extends BaseController {

    @Reference(version = "1.0.0",group = "donald")
    private GameFeatureService gameFeatureService;

    @PostMapping("/add")
    @ApiOperation("新增游戏-特性关系记录")
    @RequiresPermissions("game-feature:add:post")
    public HttpResult add(@Validated @RequestBody GameFeatureDto gameFeatureDto, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            throw new ServiceException(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }
        return gameFeatureService.add(gameFeatureDto)?toResultSuccess(InfoConstant.ADD_SUCCESS):toResultFail(InfoConstant.ADD_FAIL);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除游戏-特性关系记录")
    @RequiresPermissions("game-feature:delete:delete")
    public HttpResult delete(@Validated @RequestBody GameFeatureDto gameFeatureDto, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            throw new ServiceException(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }
        return gameFeatureService.delete(gameFeatureDto)?toResultSuccess(InfoConstant.ADD_SUCCESS):toResultFail(InfoConstant.DELETE_FAIL);
    }

    @GetMapping("/find-game")
    @ApiOperation("根据特性查询")
    public TableResult findGameByFeatureId(Long featureId){
        List<GameVo> list = gameFeatureService.findGameByFeatureId(featureId);
        return toTableResult(list);
    }

    @GetMapping("/find-feature")
    @ApiOperation("根据游戏查询特性")
    public TableResult findFeatureByGameId(Long gameId){
        List<Feature> list = gameFeatureService.findFeatureByGameId(gameId);
        return toTableResult(list);
    }
}

